<?php
namespace app\api\controller\v1;
header("Content-type: text/html; charset=utf-8");
use app\admin\model\Teacher;
use think\Db;
use ali_message\SendMessage;
use app\common\lib\aes\IAuth;
use app\common\lib\aes\Aes;
use app\api\model\User;
use app\common\lib\exception\ApiException;
use think\Log;
use think\Cache;
use app\api\model\Student as StudentModel;
use app\api\model\Teacher as TeacherModel;

class Login extends Common {
    /**
     * 新用户注册
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function register(){
        $m = new User();
        return $m->addUser();
    }

    /**
     * 效验手机号
     * @param tel string 手机号 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function check_tel(){
        if(request()->isPost()){
            $data = $_POST;
            $tel = Db::name('user')
                ->where('tel='.$data['tel'])
                ->field('id,tel')
                ->find();

            if(!empty($tel)){
                return show(config('code.error'), '手机号已注册！');
            }else{
                return show(config('code.success'), 'ok！');
            }
        }else{
            throw new ApiException('请求不合法！',401);
        }
    }

    /**
     * 记录app异常信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return void
     */
    public function write_app_exception(){
        //获取header
        $this->headers = request()->header();
        $data = $_POST;
        $data['app_type'] = $this->headers['apptype'];
        $data['did'] = $this->headers['did'];
        $data['version_code'] = $this->headers['version'];
        $data['create_time'] = time();

        try{
            model('AppException')
                ->allowField(true)
                ->save($data);
        }catch (\Exception $e){
            Log::write($e->getMessage());
        }
    }

    /**
     * 学生登录
     * @param username string  用户名 *必传
     * @param password string  密码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function student_login(){
        try{
            if(request()->isPost()){
                $data = $_POST;
                if(empty($data)) {
                    throw new ApiException('数据不合法！',401);
                }

                if(!isset($data['username'])){
                    throw new ApiException('username is not found！',404);
                }

                if(!isset($data['password'])){
                    throw new ApiException('password is not found！',404);
                }

                $data['password'] = md5($data['password']);
                $user = (new User())->where('username',$data['username'])->whereOr('tel',$data['username'])->find();

                if($user['stop_status'] == '已停用'){
                    return show(config('code.error'),'账户已停用,请联系管理员！');
                }

                $user_id = $user['id'];
                if(!empty($user)){

                    if($data['password'] != $user->password){
                        if(empty($user['password'])){
                            return show(config('code.error'),'您还未设置密码，请使用短信登录！');
                        }else{
                            return show(config('code.error'),'密码错误！');
                        }

                    }else{
                        //设置登录token
                        $token = IAuth::setAppLoginToken($user['tel']);
                        $data = [
                            'token' => $token,
                            //token过期时间
                            'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                        ];

                        $id = model('User')->save($data, ['tel' => $user['tel']]);
                        if($id){
                            if($user['user_auth'] == 3){
                                $user = (new StudentModel())->getStudentInformation($user_id);

                                $obj = new Aes();
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'学生登录成功',$user);
                            }else if($user['user_auth'] == 2){
                                return show(config('code.error'),'登录端口错误,你是教师账户，请去教师端登录!');
                            }else if($user['user_auth'] == 1){
                                return show(config('code.success'),'管理用户登录',$user);
                            }
                        }
                    }
                }else{
                    return show(config('code.error'),'账号不存在！');
                }
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

    /**
     * 手机注册会员
     * @param string tel 用户手机 *必传
     * @param int code 手机验证码 *必传
     * @param string password 用户密码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function tel_register(){
        if(request()->isPost()){
            $data = $_POST;
            if(empty($data)) {
                throw new ApiException('数据不合法！',401);
            }

            if(!isset($data['tel'])){
                throw new ApiException('tel is not found！',404);
            }

            if(!isset($data['code'])){
                throw new ApiException('code is not found！',404);
            }

            if(!isset($data['password'])){
                throw new ApiException('password is not found！',404);
            }

            if(!isset($data['user_auth'])){
                throw new ApiException('user_auth is not found！',404);
            }

            $data['password'] = md5($data['password']);

            $data['ctime'] = time();
            Db::startTrans();

            try{
                if(empty(Cache::get('code:'.$data['tel']))){
                    return show(config('code.error'), '验证码不存在或者已过期！');
                }

                if($data['code'] == Cache::get('code:'.$data['tel'])){
                    unset($data['code']);

                    Cache::rm('code:'.$data['tel']);

                    $tel = Db::name('user')
                        ->where('tel='.$data['tel'])
                        ->field('id,tel,password')
                        ->find();

                    if(!empty($tel)){
                        return show(config('code.error'), '手机号已注册！');
                    }

                    if ($id = db('user')->insertGetId($data)) {
                        if($data['user_auth'] == 2){
                            $address['member_id'] = $id;
                            db('address')->insert($address);

                            $tea['teacher_id'] = $id;
                            $res = db('teacher')->insertGetId($tea);

                            //设置登录token
                            $user = (new TeacherModel())->getTeacherInformation($id);

                            $token = IAuth::setAppLoginToken($data['tel']);
                            $data = [
                                'token' => $token,
                                //token过期时间
                                'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                            ];
                            $t_id = model('User')->save($data, ['tel' => $user['tel']]);
                            $obj = new Aes();
                            $user['token'] = $obj->encrypt($token."||".$t_id);
                            if($res){
                                Db::commit();//提交事务
                                return show(config('code.success'),'注册教师用户成功！',$user);
                            }else{
                                return show(config('code.error'),'注册教师用户失败！');
                            }
                        }else if($data['user_auth'] == 3){
                            $address['member_id'] = $id;
                            db('address')->insert($address);

                            $stu['student_id'] = $id;
                            $stu['student_rank_id'] = 1;
                            $res = db('student')->insertGetId($stu);
                            $user = (new StudentModel())->getStudentInformation($id);
                            $token = IAuth::setAppLoginToken($data['tel']);
                            $data = [
                                'token' => $token,
                                //token过期时间
                                'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                            ];
                            $t_id = model('User')->save($data, ['tel' => $user['tel']]);
                            $obj = new Aes();
                            $user['token'] = $obj->encrypt($token."||".$t_id);
                            if($res){
                                Db::commit();//提交事务
                                return show(config('code.success'),'注册学生用户成功！',$user);
                            }else {
                                return show(config('code.error'), '注册学生会员失败！');
                            }
                        }
                    }
                }else{
                    return show(config('code.error'), '验证码错误！');
                }
            }catch (\Exception $e){
                Log::write($e->getMessage());
                Db::rollback();
                throw new ApiException($e->getMessage(),500);
            }
        }
    }

    /**
     * 教师登录
     * @param username string  用户名 *必传
     * @param password string  密码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function teacher_login(){
        try{
            if(request()->isPost()){
                $data = $_POST;
                if(empty($data)) {
                    throw new ApiException('数据不合法！',401);
                }

                if(!isset($data['username'])){
                    throw new ApiException('username is not found！',404);
                }

                if(!isset($data['password'])){
                    throw new ApiException('password is not found！',404);
                }

                $data['password'] = md5($data['password']);
                $user = (new User())->where('username',$data['username'])->whereOr('tel',$data['username'])->find();
                
                if($user['stop_status'] == '已停用'){
                    return show(config('code.error'),'账户已停用,请联系管理员！');
                }

                $user_id = $user['id'];
                if(!empty($user)){

                    if($data['password'] != $user->password){
                        if(empty($user['password'])){
                            return show(config('code.error'),'您还未设置密码，请使用短信登录！');
                        }else{
                            return show(config('code.error'),'密码错误！');
                        }

                    }else{
                        //设置登录token
                        $token = IAuth::setAppLoginToken($user['tel']);
                        $data = [
                            'token' => $token,
                            //token过期时间
                            'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                        ];

                        $id = model('User')->save($data, ['tel' => $user['tel']]);
                        if($id){
                            if($user['user_auth'] == 3){
                                return show(config('code.error'),'登录端口错误，你是学生账户!请去学生登录端口登录！',$user);
                            }else if($user['user_auth'] == 2){
                                $user = (new TeacherModel())->getTeacherInformation($user_id);

                                $obj = new Aes();
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'教师登录成功',$user);
                            }else if($user['user_auth'] == 1){
                                return show(config('code.success'),'管理用户登录',$user);
                            }
                        }
                    }
                }else{
                    return show(config('code.error'),'账号不存在！');
                }
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

    /**
     * 会员登录
     * @param username string  用户名 *必传
     * @param password string  密码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function login(){
        try{
            if(request()->isPost()){
                $data = $_POST;
                if(empty($data)) {
                    throw new ApiException('数据不合法！',401);
                }

                if(!isset($data['username'])){
                    throw new ApiException('username is not found！',404);
                }

                if(!isset($data['password'])){
                    throw new ApiException('password is not found！',404);
                }

                $data['password'] = md5($data['password']);
                $user = (new User())->where('username',$data['username'])->whereOr('tel',$data['username'])->find();

                $user_id = $user['id'];
                if(!empty($user)){

                    if($data['password'] != $user->password){
                        if(empty($user['password'])){
                            return show(config('code.error'),'您还未设置密码，请使用短信登录！');
                        }else{
                            return show(config('code.error'),'密码错误！');
                        }

                    }else{
                        //设置登录token
                        $token = IAuth::setAppLoginToken($user['tel']);
                        $data = [
                            'token' => $token,
                            //token过期时间
                            'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                        ];

                        $id = model('User')->save($data, ['tel' => $user['tel']]);
                        if($id){
                            if($user['user_auth'] == 3){
                                $user = (new StudentModel())->getStudentInformation($user_id);

                                $obj = new Aes();
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'学生登录成功',$user);
                            }else if($user['user_auth'] == 2){
                                $user = (new TeacherModel())->getTeacherInformation($user_id);

                                $obj = new Aes();
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'教师登录成功',$user);
                            }else{
                                return show(config('code.success'),'非教师用户登录',$user);
                            }
                        }
                    }
                }else{
                    return show(config('code.error'),'账号不存在！');
                }
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

    /**
     * 手机号登陆
     * @param tel int 用户手机号 *必传
     * @param  code int 验证码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function tel_login(){

        try{
            if(request()->isPost()){
                $data = $_POST;
                if(empty($data)) {
                    throw new ApiException('数据不合法！',401);
                }

                if(!isset($data['tel'])){
                    throw new ApiException('tel is not found！',404);
                }

                if(!isset($data['code'])){
                    throw new ApiException('code is not found！',404);
                }

                if($data['code'] == Cache::get('code:'.$data['tel'])){
                    Cache::pull('code:'.$data['tel']);
                    $user = (new User())->where('tel',$data['tel'])->find();
                    $user_id = $user['id'];

                    if(!empty($user)){
                        //设置登录token
                        $token = IAuth::setAppLoginToken($user['tel']);
                        $data = [
                            'token' => $token,
                            //token过期时间
                            'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                        ];

                        $id = model('User')->save($data, ['tel' => $user['tel']]);
                        $obj = new Aes();
                        if($id){
                            if($user['user_auth']==3){
                                $user = (new StudentModel())->getStudentInformation($user_id);
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'学生登录成功',$user);
                            }else if($user['user_auth']==2){
                                $user = (new TeacherModel())->getTeacherInformation($user_id);
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'教师登录成功',$user);
                            }else{
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'非教师用户登录',$user);
                            }
                        }
                    }else{
                        return show(config('code.error'),'账号不存在！');
                    }
                }else{
                    return show(config('code.error'),'验证码错误！');
                }
            }

        }catch (\Exception $e){ 
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }

    }

    /**
     * 手机号教师登陆
     * @param tel int 用户手机号 *必传
     * @param  code int 验证码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function teacher_tel_login(){

        try{
            if(request()->isPost()){
                $data = $_POST;
                if(empty($data)) {
                    throw new ApiException('数据不合法！',401);
                }

                if(!isset($data['tel'])){
                    throw new ApiException('tel is not found！',404);
                }

                if(!isset($data['code'])){
                    throw new ApiException('code is not found！',404);
                }

                if($data['code'] == Cache::get('code:'.$data['tel'])){
                    Cache::pull('code:'.$data['tel']);
                    $user = (new User())->where('tel',$data['tel'])->find();
                    if($user['stop_status'] == '已停用'){
                        return show(config('code.error'),'账户已停用,请联系管理员！');
                    }

                    $user_id = $user['id'];

                    if(!empty($user)){
                        //设置登录token
                        $token = IAuth::setAppLoginToken($user['tel']);
                        $data = [
                            'token' => $token,
                            //token过期时间
                            'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                        ];

                        $id = model('User')->save($data, ['tel' => $user['tel']]);
                        $obj = new Aes();
                        if($id){
                            if($user['user_auth']==3){
                                $user = (new StudentModel())->getStudentInformation($user_id);
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.error'),'登录端口错误，你是学生账户!请去学生登录端口登录！',$user);
                            }else if($user['user_auth']==2){
                                $user = (new TeacherModel())->getTeacherInformation($user_id);
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'教师登录成功',$user);
                            }else{
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'非教师用户登录',$user);
                            }
                        }
                    }else{
                        return show(config('code.error'),'账号不存在！');
                    }
                }else{
                    return show(config('code.error'),'验证码错误！');
                }
            }

        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }

    }

    /**
     * 手机号学生登陆
     * @param tel int 用户手机号 *必传
     * @param  code int 验证码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function student_tel_login(){

        try{
            if(request()->isPost()){
                $data = $_POST;
                if(empty($data)) {
                    throw new ApiException('数据不合法！',401);
                }

                if(!isset($data['tel'])){
                    throw new ApiException('tel is not found！',404);
                }

                if(!isset($data['code'])){
                    throw new ApiException('code is not found！',404);
                }

                if($data['code'] == Cache::get('code:'.$data['tel'])){
                    Cache::pull('code:'.$data['tel']);
                    $user = (new User())->where('tel',$data['tel'])->find();

                    if($user['stop_status'] == '已停用'){
                        return show(config('code.error'),'账户已停用,请联系管理员！');
                    }

                    $user_id = $user['id'];

                    if(!empty($user)){
                        //设置登录token
                        $token = IAuth::setAppLoginToken($user['tel']);
                        $data = [
                            'token' => $token,
                            //token过期时间
                            'time_out' => strtotime("+".config('app.login_time_out_day')." days"),
                        ];

                        $id = model('User')->save($data, ['tel' => $user['tel']]);
                        $obj = new Aes();
                        if($id){
                            if($user['user_auth']==3){
                                $user = (new StudentModel())->getStudentInformation($user_id);
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'学生登录成功',$user);
                            }else if($user['user_auth']==2){
                                $user = (new TeacherModel())->getTeacherInformation($user_id);
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.error'),'登录端口错误，你是教师账户!请去教师登录端口登录！',$user);
                            }else{
                                $user['token'] = $obj->encrypt($token."||".$id);
                                return show(config('code.success'),'非教师用户登录',$user);
                            }
                        }
                    }else{
                        return show(config('code.error'),'账号不存在！');
                    }
                }else{
                    return show(config('code.error'),'验证码错误！');
                }
            }

        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }

    }

    /**
     * 发送短信验证码
     * @param  tel int  用户手机 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function send_tel_code(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法！',401);
        }

        if(!isset($data['tel'])){
            throw new ApiException('tel is not found！',404);
        }

        //实例化短信类
        $message = new SendMessage(
            "LTAI28yOwo9Y3Pgh",
            "LZ34WauMGGCAm4Un56s5dd9rwixCHI"
        );

        try{
            $code = rand(1000 ,9999);
            Cache::set('code:'.$data['tel'],$code,180);
            $date = date('Ymd');
            $message->sendSms(
                "好书换好课",        // 短信签名
                "SMS_80190250",    // 短信模板编号
                $data['tel'],      // 短信接收者
                [
                    "code" => Cache::get('code:'.$data['tel']), // 短信模板中字段的值
                ]
            );

            $send = $message->queryDetails(
                $data['tel'],         // phoneNumbers 电话号码
                $date,                // sendDate 发送时间
                10,                   // pageSize 分页大小
                1                     // currentPage 当前页码
            // "abcd"             // bizId 短信发送流水号，选填
            );
            if($send){
                return show(config('code.success'),'已发送！');
            }else{
                return show(config('code.error'),'发送失败！');
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }

    }

    /**
     * 验证手机验证码
     * @param tel string  手机号 *必传
     * @param code string  验证码 *必填
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function check_tel_code(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法！',401);
        }

        if(!isset($data['tel'])){
            throw new ApiException('tel is not found！',404);
        }

        if(!isset($data['code'])){
            throw new ApiException('code is not found！',404);
        }

        if($data['code'] == Cache::get('code:'.$data['tel'])){
            Cache::pull('code:'.$data['tel']);
            return show(config('code.success'),'验证成功！');
        }else{
            return show(config('code.error'),'验证码错误！');
        }
    }

    /**
     * 修改手机号
     * @param string tel 手机号 *必传
     * @param string code 验证码 *必填
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function change_tel(){
        if(request()->isPost()){
            $data = $_POST;
            if(empty($data)) {
                throw new ApiException('数据不合法！',401);
            }

            if(!isset($data['id'])){
                throw new ApiException('id is not found！',404);
            }

            if(!isset($data['tel'])){
                throw new ApiException('tel is not found！',404);
            }

            if(!isset($data['code'])){
                throw new ApiException('code is not found！',404);
            }

            if($data['code'] == Cache::get('code:'.$data['tel'])){
                Cache::rm('code:'.$data['tel']);

                $tel['tel'] = $data['tel'];

                $res = db('user')->where('id='.$data['id'])->update($tel);

                if($res){
                    return show(config('code.success'),'修改成功！');
                }else{
                    return show(config('code.error'),'修改失败！');
                }

            }else{
                return show(config('code.error'),'验证码错误！');
            }
        }else{
            throw new ApiException('非法请求！',500);
        }
    }

    /**
     * 用户密码修改(手机短信修改)
     * @param tel int  用户手机 *必传
     * @param password string  用户新密码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function tel_change_password(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法！',401);
        }

        if(!isset($data['tel'])){
            throw new ApiException('tel is not found！',404);
        }

        if(!isset($data['password'])){
            throw new ApiException('password is not found！',404);
        }else{
            $data['password'] = md5($data['password']);
        }

        try{
            $tel = Db::name('user')
                ->where('tel='.$data['tel'])
                ->field('id,tel,password')
                ->find();
            if(empty($tel)){
                return show(config('code.error'), '您的号码未注册');
            }else if($data['password'] == $tel['password']){
                return show(config('code.error'), '新密码与原始密码不能相同！');
            }else{
                $update = Db::name('user')
                    ->where('id='.$tel['id'])
                    ->update($data);
                if($update){
                    return show(config('code.success'), '修改成功！');
                }else{
                    return show(config('code.error'), '修改失败！');
                }
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }

    }

    /**
     * 获取用户数量
     * @author 王柯 <wangke.email@foxmail.com>
     * @return int|string
     */
    public function getUserCount(){
        $m = new User();
        return $m->getUserCount();
    }

    /**
     * 忘记密码 用户密码修改(手机短信修改)
     * @param string tel 用户手机 *必传
     * @param int code 手机验证码 *必传
     * @param string password 用户新密码 *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function tel_edit_password(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法！',401);
        }

        if(!isset($data['tel'])){
            throw new ApiException('tel is not found！',404);
        }

        if(!isset($data['code'])){
            throw new ApiException('code is not found！',404);
        }

        if(!isset($data['password'])){
            throw new ApiException('password is not found！',404);
        }else{
            $data['password'] = md5($data['password']);
        }

        try{
            if(empty(Cache::get('code:'.$data['tel']))){
                return show(config('code.error'), '验证码不存在或者已过期！');
            }

            if($data['code'] == Cache::get('code:'.$data['tel'])){
                unset($data['code']);

                Cache::rm('code:'.$data['tel']);

                $tel = Db::name('user')
                    ->where('tel='.$data['tel'])
                    ->field('id,tel,password')
                    ->find();

                if(empty($tel)){
                    return show(config('code.error'), '您的号码未注册');
                }else if($data['password'] == $tel['password']){
                    return show(config('code.error'), '新密码与原始密码不能相同！');
                }else{
                    $update = Db::name('user')
                        ->where('id='.$tel['id'])
                        ->update($data);
                    if($update){
                        return show(config('code.success'), '修改成功！',$data);
                    }else{
                        return show(config('code.error'), '修改失败！');
                    }
                }
            }else{
                return show(config('code.error'), '验证码错误！');
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }
}